package yxy.game.pm2.command.strategy;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.magicdata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.Collection;

/** 兵法 兵法遗忘 */
@MessageType(proto = Command.StrategyForget, classtype = HeroMessage.StrategyForgetRequest.class)
public class CMD_StrategyForget extends Command<HeroMessage.StrategyForgetRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.StrategyForgetRequest request) {
		String playerCode = messageSource.getTag();
		String heroCode = request.getHero();
		int pos = request.getPos();

		HeroMessage.StrategyForgetResponse.Builder response = HeroMessage.StrategyForgetResponse.newBuilder();

		try {
			if (Hero.lockHero(playerCode)) {

				// 武将判断===========================================================
				Hero hero = Hero.get(playerCode, heroCode);
				if (hero == null) {
					send(messageSource, response, Result.ParamError);
					return;
				}
				// 槽位置判断===========================================================
				String magicCode = null;
				switch (pos) {
				case 0:
					magicCode = hero.getMagicA();
					break;
				case 1:
					magicCode = hero.getMagicB();
					break;
				case 2:
					magicCode = hero.getMagicC();
					break;
				default:
					send(messageSource, response, Result.ParamError);
					return;
				}
				if (magicCode == null) {
					// 位置上没有兵法
					response.setInfo(new HeroHandle().getHeroInfo(playerCode, hero));
					send(messageSource, response, Result.Success);
					return;
				}
				// 消费===========================================================
				int dataId = Integer.valueOf(magicCode);
				magicdata data = DataCenter.getData(dataId, magicdata.class);
				if (data == null) {
					send(messageSource, response, Result.ParamError);
					return;
				}
				Currency cost = Currency.create(data.getForgetcosttype(), data.getForgetcostid(), data.getForgetcostnum());
				try {
					new CurrencyHandle().cost(playerCode, CCReason.兵法, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}
				// 获得===========================================================
				Collection<Currency> back = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
				new CurrencyHandle().addCurrency(playerCode, CCReason.兵法, back);

				// 操作===========================================================
				switch (pos) {
				case 0:
					hero.setMagicA(null);
					break;
				case 1:
					hero.setMagicB(null);
					break;
				case 2:
					hero.setMagicC(null);
					break;
				}
				new HeroHandle().updateHero(playerCode, hero);

				// 封消息===========================================================
				response.setInfo(new HeroHandle().getHeroInfo(playerCode, hero));
				send(messageSource, response, Result.Success);

			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			Hero.unlockHero(playerCode);
		}
	}

	private void send(MessageSource messageSource, HeroMessage.StrategyForgetResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}